package cn.ethan.demo.design.factory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author 张俊辉
 */

public class JDKProxy {

    public static <T> T getProxy(Class<T> interfaceClass, CacheAdapter cacheAdapter) {
        InvocationHandler jdkInvocationHandler = new JDKInvocationHandler(cacheAdapter);
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        Class<?>[] interfaces = interfaceClass.getInterfaces();
        return (T) Proxy.newProxyInstance(contextClassLoader, new Class[]{interfaces[0]}, jdkInvocationHandler);
    }
}
